import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 单链表的遍历。
 */
public class JavaDemo1 {
    public static void main(String[] args) {
        ListNode head= new ListNode(0);
        ListNode prent=head;
        for (int i=1;i<10;i++){
            ListNode body= new ListNode(i);
            prent.next=body;
            prent=body;
        }
        ListNode showParent=head;
        while (showParent!=null){
            System.out.println(showParent.val);
            showParent=showParent.next;
        }

        //
        ListNode node= ReverseList(head);
         showParent=node;
         System.out.println("result:"+node.val);
         System.out.println("result:"+node.next);
        //
        while (showParent!=null){
            System.out.println(showParent.val);
            showParent=showParent.next;
        }
    }

    private static ListNode ReverseList(ListNode head) {
        return ReverseListInt(head,null);
    }

    private static ListNode ReverseListInt(ListNode head, ListNode newHead) {
        if (head==null){
            return newHead;
        }
        ListNode next= head.next;
        head.next=newHead;
        ListNode node= ReverseListInt(next,head);
        return node;
    }

/*
    private static ListNode ReverseList(ListNode head) {
        if (head==null||head.next==null){
            return head;
        }
        ListNode reverse= ReverseList(head.next);
        head.next.next=head;
        head.next=null;
        return reverse;
    }
*/



/*    private static ListNode ReverseList(ListNode head) {
        ListNode newHead=null;
        while (head!=null){
           ListNode temp= head.next;
           head.next=newHead;
           newHead=head;
           head=temp;
        }
        return newHead;
    }*/

/*
    private static ListNode ReverseList(ListNode head) {
        Stack<ListNode> stack=new Stack<>();
        while (head!=null){
            stack.push(head);
            head=head.next;
        }
        if (stack.isEmpty()){
            return null;
        }
        ListNode dummy=stack.pop();
        ListNode node=dummy;
        while (!stack.isEmpty()){
           ListNode tempNode= stack.pop();
           dummy.next=tempNode;
           dummy=tempNode;
        }
        dummy.next=null;
        return node;
    }
*/

   /* static public ListNode ReverseList(ListNode head) {
        List<ListNode> nodes=new ArrayList<>();
        while (head!=null){
            nodes.add(head);
            head=head.next;
        }
        head= nodes.get(nodes.size()-1);
        ListNode prent=head;
        for (int i=nodes.size()-2;i>-1;i--){
            prent.next=nodes.get(i);
            prent=nodes.get(i);
            if (i==0){
                prent.next=null;
            }
        }
        return head;
    }*/



}

